<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\cms\controller\admin;

use app\admin\common\AdminController;

class Slider extends AdminController
{
    public function index()
    {
        $pagination = $this->_get_pagination_data();

        $pagination['rows_count'] = model('cms/slider')
        ->where($pagination['where_map'])->count();

        $pagination['total_pages'] = ceil($pagination['rows_count'] / $pagination['page_size']);
        $pagination['page_num'] = $pagination['page_num'] > $pagination['total_pages'] ? 1 : $pagination['page_num'];

        $slider_list = model('cms/slider')
        ->where($pagination['where_map'])
        ->page($pagination['page_num'], $pagination['page_size'])
        ->order($pagination['sort_field'], $pagination['sort_order'])
        ->select();

        $this->assign('slider_list', $slider_list);
        $this->assign('pagination', $pagination);
        $this->display('admin/slider/slider_list');
    }

    public function create()
    {
        $slider_info = model('cms/slider')->initDefault();

        if (request()->isPost()) {
            $slider_info->acceptData($this->post_array());

            if ($slider_info->save() !== false) {
                $this->success('新增成功。', $this->url('index'));
            }
        }

        $this->assign('slider_info', $slider_info);
        $this->display('admin/slider/slider_form');
    }

    public function modify($id)
    {
        $slider_info = model('cms/slider')->findOrFail($id);

        if (request()->isPost()) {
            $slider_info->acceptData($this->post_array());

            if ($slider_info->save() !== false) {
                $this->success('修改成功。', $this->url('index'));
            }
        }

        $this->assign('slider_info', $slider_info);
        $this->display('admin/slider/slider_form');
    }

    public function bulk_delete($ids = array())
    {
        $slider_list = model('cms/slider')->where('id', 'in', $ids)->select();

        foreach ($slider_list as $slider_info) {
            $slider_info->delete();
        }

        $this->success('删除成功。', $this->url('index'));
    }
}
